//
//  EpointExtendAppListViewController.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-3-28.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointExtendAppListViewController.h"
#import "EpointMainLoadTask.h"
#import "EpointMainConfigModel.h"
#import "EpointExtendAppTableViewCell.h"
#import "HJObjManager.h"
#import "EpointExtendAppConfig.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"

@interface EpointExtendAppListViewController ()
{
    UITableView *tv;
    NSMutableArray *dataExtendArr;
    HJObjManager* objMan;
}
@end

@implementation EpointExtendAppListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *bs = [[NSUserDefaults standardUserDefaults]objectForKey:Epoint_User_MainConfig];
    dataExtendArr = [EpointMainLoadTask getArray:bs default:@"2"];
    
    tv = [[UITableView alloc]initWithFrame:[self getFullContentFrame]];
    
    tv.dataSource = self;
    tv.delegate = self;
    tv.backgroundView = nil;
    
    [self.view addSubview:tv];
    
    objMan = [[HJObjManager alloc] initWithLoadingBufferSize:20 memCacheSize:20];
    NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/imgcache/flickr/"] ;
    HJMOFileCache* fileCache = [[HJMOFileCache alloc] initWithRootPath:cacheDirectory];
    objMan.fileCache = fileCache;
    fileCache.fileCountLimit = 100;
    fileCache.fileAgeLimit = 60*60*24*7; //1 week
    [fileCache trimCacheUsingBackgroundThread];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"EpointExtendAppTableViewCell";
    EpointExtendAppTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"EpointExtendAppTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
    }
    EpointMainConfigModel *m = [dataExtendArr objectAtIndex:indexPath.row];
    
    EpointExtendAppConfig *cg = (EpointExtendAppConfig *)[EpointExtendAppConfig findFirstByCriteria:[NSString stringWithFormat:@"WHERE moduleid = '%@'",m.moduleID]];
    
    if ([cg.isuseradd isEqualToString:@"1"]) {
        [cell.btAdd setTitle:@"已添加" forState:UIControlStateNormal];
        cell.handler = ^{
            
            RIButtonItem *okItem = [RIButtonItem item];
            okItem.label = @"确定";
            okItem.action = ^
            {
                [cg deleteObject];
                [tv reloadData];
                [DBFrameService setConfigValue:@"APPEXTENDCHANGED" value:@"1"];
            };
            
            RIButtonItem *cancelItem = [RIButtonItem item];
            cancelItem.label = @"取消";
            cancelItem.action = nil;
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"需要移除该扩展应用吗？" cancelButtonItem:cancelItem otherButtonItems:okItem,nil];
            [alter show];
            
        };
    }
    else
    {
        [cell.btAdd setTitle:@"添加" forState:UIControlStateNormal];
        cell.handler = ^{
            
            EpointExtendAppConfig *config = [[EpointExtendAppConfig alloc]init];
            config.moduleid = m.moduleID;
            config.type = m.type;
            config.isdefault = m.isDefault;
            config.modulename = m.modulename;
            config.position = m.position;
            config.platform = m.platform;
            config.dyurl = m.dyUrl;
            config.padata = m.paData;
            config.iconurl = m.iconUrl;
            config.apkurl = m.apkUrl;
            config.launchclass = m.launchclass;
            config.packagename = m.packagename;
            config.version = m.version;
            config.isuseradd = @"1";
            config.userguid = [DBFrameService getUserGuid];
            [config save];
            [tv reloadData];
            [DBFrameService setConfigValue:@"APPEXTENDCHANGED" value:@"1"];
        };
    }
    cell.lbTitle.text = m.modulename;
    
    cell.ivIcon.url = [NSURL URLWithString:[EpointBizTools getUrl:[NSString stringWithFormat:@"mobileconfig/mainimg/%@",m.iconUrl]]];
    [objMan manage:cell.ivIcon];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataExtendArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 62;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
